package com.financing.controller.invest;
import java.util.Date;

import com.financing.enums.invest.CouponRule;
import com.financing.model.Data;
import com.financing.model.invest.Coupon;
import com.financing.service.invest.CouponService;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.core.Controller;

public class CouponController extends Controller {


	private CouponService coupon = new CouponService();

	
	/**
	 * 优惠券列表
	 */
	public void list() { 
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		renderJson(Json.ok("数据获取成功！", Data.put("page",coupon.list(page,size,Data.flat(getParaMap())))));
	}
	
	/**
	 * 编辑优惠券信息
	 */
	public void edit() {
		
		int id = getParaToInt("id",0);
		String name = getPara("name");
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		Date validStartTime = getParaToDate("validStartTime");
		Date validEndTime = getParaToDate("validEndTime");
		double num = Double.valueOf(getPara("num","0.0"));
		String rule = getPara("rule");
		int credit = getParaToInt("credit",0);
		Coupon c = new Coupon();
		String limitAmount = getPara("limitAmount","");
		
		if(Str.nb(startTime) && Str.nb(endTime) && Str.nb(limitAmount)
			&& Str.nb(rule) && Str.nb(rule) && Str.nb(name) && num>0) {
			if(id>0){
				c = Coupon.me.findById(id);
			}else{
				if(coupon.findByName(getPara("name"))!=null){
					renderJson(Json.error("优惠券名称不可重复！"));
					return;
				}
			}
			if(startTime.compareTo(endTime)>0) {
				renderJson(Json.error("活动开始日期不可大于结束日期！"));
				return;
			}
			if(validStartTime!=null && validEndTime!=null &&validStartTime.compareTo(validEndTime)>0) {
				renderJson(Json.error("有效期开始日期不可大于结束日期！"));
				return;
			}
			if(Str.eq(CouponRule.EXCHANGE, rule) ) {
				if(credit<=0) {
					renderJson(Json.error("兑换所需积分不可小于0！"));
					return;
				}
			}
			c.setName(name);
			c.setNum(num);
			c.setApplyNum(0.0);
			c.setLimitAmount(limitAmount);
			c.setType(getPara("type"));
			c.setStartTime(DateUtil.parse(startTime));
			c.setEndTime(DateUtil.parse(endTime));
			c.setValidDay(getParaToInt("validDay",0));
			c.setCredit(credit);
			c.setValidStartTime(validStartTime);
			c.setValidEndTime(validEndTime);
			c.setStatus(getPara("status")); 
			c.setSummary(getPara("summary"));
			c.setAllowBid(","+getPara("allowBid")+",");
			c.setRule(rule);
			c.setRuleAmount(getPara("ruleAmount"));
			c.setIsRepeat(getParaToInt("isRepeat"));
			c.setLink(getPara("link"));
			c.setRuleBid(","+getPara("ruleBid")+",");

			if(id>0 ? c.update() : c.setCreateTime(new Date()).save()){
				renderJson(Json.ok("优惠券信息编辑成功！"));
			}else{
				renderJson(Json.error("优惠券信息编辑失败！"));
			}
		}else {
			renderJson(Json.error("数据不可为空！"));
			return;
		}
	}
	
	/**
	 * 查看优惠券
	 */
	public void info() {
		int id = getParaToInt("id");
		Coupon c = Coupon.me.findById(id);
		coupon.transTagName(c);
		renderJson(Json.ok("数据获取成功！", Data.put("item",c)));
	}

	/**
	 * 查看优惠券
	 */
	public void getNameByType(String type) {
		int id = getParaToInt("id");
		renderJson(Json.ok("数据获取成功！", Data.put("item",Coupon.me.findById(id))));
	}

	/**
	 * 停用优惠券
	 */
	public void stop() {
		int id = getParaToInt("id");
		Coupon.me.findById(id).setStatus("stop").update();
		renderJson(Json.ok("停用优惠券成功！"));
	}
}
 